var tuiwa = require("../../tuiwa.js");
var lasttime = 0;
Page({
    data: {
        choose: 0,
        timu: [],
        testResultBg: "",
        title: "",
        testResultList: [],
        progress: 0,
        id: null,
        lookResultLayer: false,
        choiceArray: [],
        multipleChoiceArr: [],
        cunwanAnswer: [],
        authenticAnswers: []
    },
    onLoad: function(options) {
        console.log(options)
        tt.showShareMenu();

        let timu = JSON.parse(options.timu)
        console.log(timu);
        console.log(options);
        wx.setNavigationBarTitle({
            title: options.title
        })
        this.setData({
            title: options.title,
            timu: JSON.parse(options.timu),
            choose: options.id,
            desc: options.desc,
            goodsId: options.goodsId
        })
        tt.setStorageSync('g_id', this.data.goodsId);
        tuiwa.globalData.currentPage = this
        console.log(this.data.choose);
    },
    sex(e) {
        // console.log(e.currentTarget.dataset.value);
        this.setData({
            choose: 1,
            sex: e.currentTarget.dataset.value
        })
    },
    choose(e) {
        // console.log(e.currentTarget.dataset.value);
        let kcal = e.currentTarget.dataset.value
        console.log(kcal);
    },
    itemClick(e) {
        var index = e.currentTarget.dataset.fs
        var indexArr = this.data.choiceArray
        indexArr.push(index)

        this.setData({
            id: index,
            choiceArray: indexArr // choiceArray用来存放选择的选项
        })
        console.log(this.data.choiceArray)

        if ((this.data.progress + 1) == this.data.timu.length) { // 答完题跳转
            let sum = 0;
            let arr = this.data.choiceArray
            arr.forEach(item => {
                sum = sum + item
            })
            console.log(sum, 'sum跳转');
            this.setData({
                lookResultLayer: true,
                // choose: 3,  // 展示页面
            })
            if (sum < 20) {
                this.setData({
                    health: 0
                })
            } else if (sum > 20 && sum < 39) {
                this.setData({
                    health: 1
                })
            } else if (sum > 39) {
                this.setData({
                    health: 2
                })
            }
            return
        }
        // setTimeout(() => {
        this.setData({
            id: null,
            progress: this.data.progress + 1
        });
        // }, 1000)
    },
    close() {
        this.setData({
            lookResultLayer: false,
        })
    },
    toResult() {
        tuiwa.playAd();
    },
    backPreviousQuestionClick() {
        let d = new Date();
        let nowtime = d.getTime();
        if (nowtime - lasttime > 1500) {
            this.data.choiceArray.pop()
                // setTimeout(() => {
            this.setData({
                    id: null,
                    progress: this.data.progress - 1
                })
                // }, 500)

            lasttime = nowtime;
        }
        console.log("choiceArray:", this.data.choiceArray)
    },
    itemMultipleClick(e) {
        console.log(e.currentTarget.dataset);
        var kcal = e.currentTarget.dataset.kcal
        var index = e.currentTarget.dataset.index
        console.log("index的值是：", index)
        console.log(this.data.progress)
        var item = this.data.timu[this.data.progress].options[index]
        item.flag1 = !item.flag1

        var multipleChoiceArr = this.data.multipleChoiceArr
        if (item.flag1) {
            multipleChoiceArr.push(kcal)
        } else {
            multipleChoiceArr.splice(index, 1)
        }

        this.setData({
            timu: this.data.timu,
            multipleChoiceArr
        })
        console.log("multipleChoiceArr的值是1111:", this.data.multipleChoiceArr)
    },
    answerNextQuestionClick() {
        if ((this.data.progress + 1) == this.data.timu.length) {
            let sum = 0;
            let arr = this.data.multipleChoiceArr
            arr.forEach(item => {
                sum = sum + item
            })
            console.log(sum);
            if (this.data.sex == 0) {
                console.log('nan');
                if (sum < 1499) {
                    this.setData({
                        health: 0
                    })
                } else if (sum > 1499 && sum < 2199) {
                    this.setData({
                        health: 1
                    })
                } else if (sum > 2199) {
                    this.setData({
                        health: 2
                    })
                }
            } else if (this.data.sex == 1) {
                console.log('nv');
                if (sum < 1499) {
                    this.setData({
                        health: 0
                    })
                } else if (sum > 1499 && sum < 1999) {
                    this.setData({
                        health: 1
                    })
                } else if (sum > 1999) {
                    this.setData({
                        health: 2
                    })
                }
            }
            this.setData({
                choose: 202,
            })
            return
        }
        // setTimeout(() => {
        this.setData({
            progress: this.data.progress + 1
        });
        // }, 1000)
    },
    next() {
        this.setData({
            mistaken: false,
            id: null,
            progress: this.data.progress + 1
        })
        if (this.data.progress == this.data.timu.length) {
            console.log('题目做完跳转');
            this.setData({
                choose: 2,
                jianfei: true
            })
            return
        }
    },
    next1() {
        this.setData({
            mistaken: false,
            id: null,
            answerNum: false,
            choose: 11,
            progress: this.data.progress + 1
        })
        if (this.data.progress == this.data.timu.length) {
            console.log('题目做完跳转');
            this.setData({
                choose: 2,
                jianfei: true
            })
            return
        }
    },
    answer(e) {
        tuiwa.playAd();
        // console.log(e.currentTarget.dataset.value);
        this.setData({
            answerT: e.currentTarget.dataset.value,
        })
    },
    closeAdFunction: function() {
        console.log("看完了");
        if (this.data.choose == 11) {
            this.setData({
                answer: this.data.answerT,
                choose: -1,
                mistaken: false,
                answerNum: true
            })
        } else if (this.data.choose == 111) {
            this.setData({
                lookResultLayer: false,
                choose: 3, // 展示页面
            })
        }
    },
    cancelAdFunction: function() {
        console.log("没看完广告");
        tt.showModal({
            title: "温馨提示",
            content: "要观看完整视频才能进入哦~",
            showCancel: !1
        })
    },
    itemClick1(e) {
        if (e.currentTarget.dataset.flag != true) {
            console.log('回答错误');
            this.setData({
                mistaken: true,
                answer: e.currentTarget.dataset.answer,
            })
        } else {
            var index = e.currentTarget.dataset.kcal;
            // var indexArr = this.data.choiceArray
            // indexArr.push(index)

            this.setData({
                id: index,
                // choiceArray: indexArr // choiceArray用来存放选择的选项
            })
            console.log(this.data.choiceArray)
            if ((this.data.progress + 1) == this.data.timu.length) {
                console.log('题目做完跳转');
                this.setData({
                    choose: 2,
                    jianfei: true
                })
                return
            }
            this.setData({
                id: null,
                progress: this.data.progress + 1
            });
        }

    },
    onShareAppMessage: function(shareOption) {
        // console.log('onShareAppMessage');
        let that = this
            // 右上角分享
        console.log('goodsId', this.data.goodsId);
        let shareUserId = tt.getStorageSync('dyUser').id;
        console.log('goodsId', this.data.goodsId);
        console.log(tt.getStorageSync('dyUser').uid)
        return {
            templateId: '', //分享的模版 id
            title: this.data.desc, //分享的标题
            desc: this.data.desc, // 分享的内容
            path: `/pages/loading/loading?goodsId=` + that.data.goodsId + "&shareUserId=" + tt.getStorageSync('dyUser').uid + "&mId=" + tt.getStorageSync('dyUser').id, // 分享的路径
        }
    },
})